Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Sys = System
Imports Tex = System.Text

Namespace Enhancements.Core.Logging
    ''' <summary>An enumeration of values that can be used as markers for real folders.</summary>
    Public NotInheritable Class TokenPath

        Private Const __Prefix As System.String = "|"
        Private Const __Suffix As System.String = "|"

        Private __Value As System.String = Nothing
        ''' <summary>The marker to use.</summary>
        Public Property Value() As System.String
            Get
                Return __Value
            End Get
            Private Set(ByVal _value As System.String)
                __Value = _value
            End Set
        End Property

        ''' <summary>The user temporary files folder.</summary>
        Public Shared ReadOnly TemporaryUserFolder As TokenPath = New TokenPath(0)
        ''' <summary>The user temporary files folder root.</summary>
        Public Shared ReadOnly TemporaryUserFolderDrive As TokenPath = New TokenPath(1)
        ''' <summary>The user roaming profile files folder.</summary>
        Public Shared ReadOnly RoamingProgramUserFolder As TokenPath = New TokenPath(2)
        ''' <summary>The user roaming profile files folder root.</summary>
        Public Shared ReadOnly RoamingProgramUserFolderDrive As TokenPath = New TokenPath(3)
        ''' <summary>The system program files folder.</summary>
        Public Shared ReadOnly ProgramFolder As TokenPath = New TokenPath(4)
        ''' <summary>The system program files folder root.</summary>
        Public Shared ReadOnly ProgramFolderDrive As TokenPath = New TokenPath(5)
        ''' <summary>The user desktop files folder.</summary>
        Public Shared ReadOnly UserDesktopFolder As TokenPath = New TokenPath(6)
        ''' <summary>The user desktop files folder root.</summary>
        Public Shared ReadOnly UserDesktopFolderDrive As TokenPath = New TokenPath(7)
        ''' <summary>The user internet cache files folder.</summary>
        Public Shared ReadOnly InternetCacheUserFolder As TokenPath = New TokenPath(8)
        ''' <summary>The user internet cache files folder root.</summary>
        Public Shared ReadOnly InternetCacheUserFolderDrive As TokenPath = New TokenPath(9)
        ''' <summary>The user program files data files folder.</summary>
        Public Shared ReadOnly ProgramUserFolder As TokenPath = New TokenPath(10)
        ''' <summary>The user program files data files folder root.</summary>
        Public Shared ReadOnly ProgramUserFolderDrive As TokenPath = New TokenPath(11)
        ''' <summary>The user documents files folder.</summary>
        Public Shared ReadOnly DocumentsUserFolder As TokenPath = New TokenPath(12)
        ''' <summary>The user documents files folder root.</summary>
        Public Shared ReadOnly DocumentsUserFolderDrive As TokenPath = New TokenPath(13)
        ''' <summary>The current folder.</summary>
        Public Shared ReadOnly CurrentFolder As TokenPath = New TokenPath(14)
        ''' <summary>The current folder root.</summary>
        Public Shared ReadOnly CurrentFolderDrive As TokenPath = New TokenPath(15)

        Private Sub New(ByVal _index As Sys.Int32)
            Dim _value As String = Sys.String.Empty

            _value += __Prefix
            Select Case _index
                Case 0 : _value += "TemporaryUserFolder"
                Case 1 : _value += "TemporaryUserFolderDrive"
                Case 2 : _value += "RoamingProgramUserFolder"
                Case 3 : _value += "RoamingProgramUserFolderDrive"
                Case 4 : _value += "ProgramFolder"
                Case 5 : _value += "ProgramFolderDrive"
                Case 6 : _value += "UserDesktopFolder"
                Case 7 : _value += "UserDesktopFolderDrive"
                Case 8 : _value += "InternetCacheUserFolder"
                Case 9 : _value += "InternetCacheUserFolderDrive"
                Case 10 : _value += "ProgramUserFolder"
                Case 11 : _value += "ProgramUserFolderDrive"
                Case 12 : _value += "DocumentsUserFolder"
                Case 13 : _value += "DocumentsUserFolderDrive"
                Case 14 : _value += "CurrentFolder"
                Case 15 : _value += "CurrentFolderDrive"
            End Select
            _value += __Suffix

            Value = _value
        End Sub

        Friend Shared Function ___Parse(ByVal _value As String) As System.String
            ___Assert.___IsEmptyNot("_value", _value, True)

            Dim _returnValue As System.String = Nothing

            Select Case _value.ToUpperInvariant()
                Case TemporaryUserFolder.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetTempPath()
                Case TemporaryUserFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Iou.Path.GetTempPath())
                Case RoamingProgramUserFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
                Case RoamingProgramUserFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
                Case ProgramFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
                Case ProgramFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
                Case UserDesktopFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                Case UserDesktopFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
                Case InternetCacheUserFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
                Case InternetCacheUserFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
                Case ProgramUserFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
                Case ProgramUserFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
                Case DocumentsUserFolder.Value.ToUpperInvariant()
                    _returnValue = Sys.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                Case DocumentsUserFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
                Case CurrentFolder.Value.ToUpperInvariant()
                    _returnValue = Iou.Directory.GetCurrentDirectory()
                Case CurrentFolderDrive.Value.ToUpperInvariant()
                    _returnValue = Iou.Path.GetPathRoot(Iou.Directory.GetCurrentDirectory())
            End Select

            ___Assert.___IsEmptyNot("_returnValue", _returnValue, True)
            Return _returnValue
        End Function

        Friend Shared Function ___RemoveAllOccurrences(ByVal _value As System.String) As System.String
            ___Assert.___IsNullNot("_value", _value, True)

            Dim _returnValue As System.String = New Tex.StringBuilder(_value).ToString()

            _returnValue = _returnValue.Replace(TokenPath.CurrentFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.CurrentFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.DocumentsUserFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.DocumentsUserFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.InternetCacheUserFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.InternetCacheUserFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.ProgramFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.ProgramFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.ProgramUserFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.ProgramUserFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.RoamingProgramUserFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.RoamingProgramUserFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.TemporaryUserFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.TemporaryUserFolderDrive.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.UserDesktopFolder.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenPath.UserDesktopFolderDrive.Value, Sys.String.Empty)

            Return _returnValue
        End Function

        Friend Shared Function ___ReplaceAllOccurrences(ByVal _value As System.String) As System.String
            ___Assert.___IsNullNot("_value", _value, True)

            Dim _returnValue As System.String = New Tex.StringBuilder(_value).ToString()

            _returnValue = _returnValue.Replace(TokenPath.TemporaryUserFolder.Value, Iou.Path.GetTempPath())
            _returnValue = _returnValue.Replace(TokenPath.TemporaryUserFolderDrive.Value, Iou.Path.GetPathRoot(Iou.Path.GetTempPath()))
            _returnValue = _returnValue.Replace(TokenPath.RoamingProgramUserFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
            _returnValue = _returnValue.Replace(TokenPath.RoamingProgramUserFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)))
            _returnValue = _returnValue.Replace(TokenPath.ProgramFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
            _returnValue = _returnValue.Replace(TokenPath.ProgramFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)))
            _returnValue = _returnValue.Replace(TokenPath.UserDesktopFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
            _returnValue = _returnValue.Replace(TokenPath.UserDesktopFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)))
            _returnValue = _returnValue.Replace(TokenPath.InternetCacheUserFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
            _returnValue = _returnValue.Replace(TokenPath.InternetCacheUserFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)))
            _returnValue = _returnValue.Replace(TokenPath.ProgramUserFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
            _returnValue = _returnValue.Replace(TokenPath.ProgramUserFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)))
            _returnValue = _returnValue.Replace(TokenPath.DocumentsUserFolder.Value, Sys.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
            _returnValue = _returnValue.Replace(TokenPath.DocumentsUserFolderDrive.Value, Iou.Path.GetPathRoot(Sys.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)))
            _returnValue = _returnValue.Replace(TokenPath.CurrentFolder.Value, Iou.Directory.GetCurrentDirectory())
            _returnValue = _returnValue.Replace(TokenPath.CurrentFolderDrive.Value, Iou.Path.GetPathRoot(Iou.Directory.GetCurrentDirectory()))

            Return _returnValue
        End Function

    End Class
End Namespace
